package lj.courseinterect.dao;

import lj.courseinterect.dao.i.AnswerInfoDao;
import lj.courseinterect.dao.i.QuestionInfoDao;
import lj.courseinterect.entity.AnswerInfo;
import lj.courseinterect.entity.QuestionInfo;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Root;
import java.util.List;

public class JpaAnswerInfoDao extends JpaDao<AnswerInfo, Long> implements AnswerInfoDao{

    public JpaAnswerInfoDao() {
        super(AnswerInfo.class);
    }



    @Override
    @Transactional(readOnly = true)

    public List<AnswerInfo> findByQueId(Long queId) {
        final CriteriaBuilder builder = this.getEntityManager().getCriteriaBuilder();
        final CriteriaQuery<AnswerInfo> criteriaQuery = builder.createQuery(this.entityClass);

        Root<AnswerInfo> root = criteriaQuery.from(this.entityClass);
        Path<String> namePath = root.get("queId");
        criteriaQuery.where(builder.equal(namePath, queId)).orderBy(builder.asc(root.get("ansTime")));


        TypedQuery<AnswerInfo> typedQuery = this.getEntityManager().createQuery(criteriaQuery);
        List<AnswerInfo> answerInfos = typedQuery.getResultList();

        if (answerInfos.isEmpty()) {
            return null;
        }
        return answerInfos;
    }

    @Override
    public List<AnswerInfo> findByStuId(Long stuId) {
        final CriteriaBuilder builder = this.getEntityManager().getCriteriaBuilder();
        final CriteriaQuery<AnswerInfo> criteriaQuery = builder.createQuery(this.entityClass);

        Root<AnswerInfo> root = criteriaQuery.from(this.entityClass);
        Path<String> namePath = root.get("stuId");
        criteriaQuery.where(builder.equal(namePath, stuId)).orderBy(builder.asc(root.get("ansTime")));


        TypedQuery<AnswerInfo> typedQuery = this.getEntityManager().createQuery(criteriaQuery);
        List<AnswerInfo> answerInfos = typedQuery.getResultList();

        if (answerInfos.isEmpty()) {
            return null;
        }
        return answerInfos;
    }

    @Override
    public List<AnswerInfo> findByTeaId(Long teaId) {
        final CriteriaBuilder builder = this.getEntityManager().getCriteriaBuilder();
        final CriteriaQuery<AnswerInfo> criteriaQuery = builder.createQuery(this.entityClass);

        Root<AnswerInfo> root = criteriaQuery.from(this.entityClass);
        Path<String> namePath = root.get("teaId");
        criteriaQuery.where(builder.equal(namePath, teaId)).orderBy(builder.asc(root.get("ansTime")));


        TypedQuery<AnswerInfo> typedQuery = this.getEntityManager().createQuery(criteriaQuery);
        List<AnswerInfo> answerInfos = typedQuery.getResultList();

        if (answerInfos.isEmpty()) {
            return null;
        }
        return answerInfos;
    }

    @Override
    public List<AnswerInfo> queryByQueIdOrderByGood(Long queId) {
        final CriteriaBuilder builder = this.getEntityManager().getCriteriaBuilder();
        final CriteriaQuery<AnswerInfo> criteriaQuery = builder.createQuery(this.entityClass);

        Root<AnswerInfo> root = criteriaQuery.from(this.entityClass);
        Path<String> namePath = root.get("queId");
        criteriaQuery.where(builder.equal(namePath, queId)).orderBy(builder.desc(root.get("goodCount")));


        TypedQuery<AnswerInfo> typedQuery = this.getEntityManager().createQuery(criteriaQuery);
        List<AnswerInfo> answerInfos = typedQuery.getResultList();

        if (answerInfos.isEmpty()) {
            return null;
        }
        return answerInfos;
    }
}
